/**
 * Created by linxh on 2015/9/9.
 */
define("framework/validation/TooltipMessengerConstructor", [
    'framework/validation/AbstractIllegalMessenger'
], function (AbstractIllegalMessenger) {
    return function (GillionTooltipService) {
        var ILLEGAL_MSG_KEY = '$illegal-msg';
        var TOOLTIP_DOM="$TOOLTIP_DOM";

        function TooltipMessenger (){
        }

        TooltipMessenger.prototype = AbstractIllegalMessenger.prototype;


        TooltipMessenger.prototype.handle = function (msgParams) {

            var $target = msgParams.$verifyTarget,
                $outerContainer = msgParams.$outerContainer,
                ruleName = msgParams.ruleName,
                message = msgParams.message,
                illegalMessage = $target.data(ILLEGAL_MSG_KEY) || {},
                outputMsg, messages,
                $focus, tooltip,
                msgKey, params;
            if($target[0].tagName=="INPUT" || $target[0].tagName=='A'){
                $focus = $target;
            }else{
                $focus = $outerContainer.find("a");
            }
            if($focus.length==0){
                $focus = $outerContainer;
            }
            if (msgParams.isValid) {
                if(illegalMessage.hasOwnProperty(ruleName))
                    delete illegalMessage[ruleName];
            } else {
                illegalMessage[ruleName] = message;
            }
            for (msgKey in illegalMessage) {
                outputMsg = illegalMessage[msgKey];
                break;
            }
            if (!!outputMsg) {
                params = this.InitParam(outputMsg, "warn");
                messages = GillionTooltipService.create($outerContainer, params);
                messages.createMessageDom();
                tooltip = $outerContainer.data(TOOLTIP_DOM);
                $outerContainer.addClass("high-light-border");
                tooltip.css("display","none");
                $focus.on("focus",function(){
                    if($outerContainer.data(TOOLTIP_DOM)){
                        tooltip.css("display","block");
                        messages.locate(messages.element);
                    }
                });
                $focus.on("blur",function(){
                    tooltip.css("display","none");
                });
                $outerContainer.on("mouseenter",function(){
                    if($outerContainer.data(TOOLTIP_DOM)){
                        tooltip.css("display","block");
                        messages.locate(messages.element);
                    }
                });
                $outerContainer.on("mouseleave",function(){
                    if(document.activeElement != $focus[0]){
                        tooltip.css("display","none");
                    }
                });
                if(this.validaForm){
                    $focus[0].focus();
                    this.validaForm = false;
                }else if(document.activeElement == $focus[0]){
                    if($outerContainer.data(TOOLTIP_DOM)){
                        tooltip.css("display","block");
                        messages.locate(messages.element);
                    }
                }
            } else {
                GillionTooltipService.create($outerContainer).remove(0);
                $outerContainer.removeClass("high-light-border");
            }
            $target.data(ILLEGAL_MSG_KEY, illegalMessage);
        };

        TooltipMessenger.prototype.clear = function ($target) {
            var params = {};
            $target.removeData(ILLEGAL_MSG_KEY);
            $target.removeClass("high-light-border");
            GillionTooltipService.create($target, params).remove();
        };

        TooltipMessenger.prototype.startValidForm = function () {
            this.validaForm = true;
        };

        TooltipMessenger.prototype.InitParam = function (message, messageType) {
            var params = {
                'gTooltip': message,
                'outterBox': true,
                'tipBoxDefaultEvent': false
            };
            if (messageType == 'tip') {
                params.fontColor = "#00b7ee";
                params.iconColor = "#00b7ee";
                params.iconType = 'fi-help';
            } else if (messageType == 'warn') {
                params.fontColor = "#E0600B";
                params.iconColor = "#f06e00";
                params.iconType = 'fi-error';
            }
            return params;
        };

        return new TooltipMessenger();
    };
});
